package com.frxuexi.repair.view

import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.base.entity.RepairBeanItem
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.base.utils.ArouterManager
import com.frxuexi.repair.adapter.RepairAdapter
import com.frxuexi.repair.RepairIntent
import com.frxuexi.repair.RepairViewModel
import com.frxuexi.repair.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairIntent, RepairViewModel>() {

    var flag = false
    val list = mutableListOf<RepairBeanItem>()
    val repairAdapter by lazy { RepairAdapter() }
    var position=-1
    override fun getVM(): RepairViewModel {
        return viewModels<RepairViewModel>().value
    }

    override fun getBind(): ActivityRepairBinding {
        return ActivityRepairBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.DEFAULT -> {
                                repairAdapter.data.clear()
                                list.clear()
                                list.addAll(it.data as Collection<RepairBeanItem>)
                                repairAdapter.addData(list.sortedByDescending { it.id })
                                repairAdapter.notifyDataSetChanged()
                            }

                            RequestType.ITEM -> {
                                repairAdapter.data.clear()
                                val item = it.data as RepairBeanItem
                                repairAdapter.addData(item)
                                repairAdapter.notifyDataSetChanged()
                            }

                            RequestType.HOME -> {
                                repairAdapter.data.clear()
                                val items=it.data as Collection<RepairBeanItem>
                                if (items.size != 0) {
                                    repairAdapter.addData(items)
                                    repairAdapter.notifyDataSetChanged()
                                } else {
                                    vm.sendIntent(RepairIntent.getRepairByNum(binding.repairSv.text.toString()))
                                }
                            }
                            RequestType.DELETE->{
                                repairAdapter.notifyItemRemoved(position)
                            }
                            else -> {}
                        }
                    }
                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.headRepair.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("维修管理")
            headAdd.setOnClickListener {
                ArouterManager.navigation(this@RepairActivity,"/repair/RepariAddActivity")
            }
        }

        binding.rvRepair.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.navigation(this,"/repair/RepairDetailActivity","rid",repairAdapter.data.get(position).id.toString())
        }

        repairAdapter.setOnItemLongClickListener { adapter, view, position ->
            this.position=position
            vm.sendIntent(RepairIntent.deleteRepair(repairAdapter.data.get(position).id!!))
            return@setOnItemLongClickListener true
        }

        binding.repairOrder.setOnClickListener {
            repairAdapter.apply {
                data.clear()
                if (flag) {
                    addData(list.sortedBy { it.repairTime })
                    binding.repairOrderiv.setImageResource(com.frxuexi.base.R.drawable.ic_action_asc)
                } else {
                    addData(list.sortedByDescending { it.repairTime })
                    binding.repairOrderiv.setImageResource(com.frxuexi.base.R.drawable.ic_action_down)
                }
                notifyDataSetChanged()
            }
            flag = !flag
        }

        binding.btnSearch.setOnClickListener {
            val content = binding.repairSv.text.toString()
            if (content.isNotEmpty())
                vm.sendIntent(RepairIntent.getRepairByName(content))
//
        }

        binding.repairSv.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s.isNullOrEmpty()){
                    repairAdapter.apply {
                        data.clear()
                        addData(list)
                        notifyDataSetChanged()
                    }
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }
        })

    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(RepairIntent.getRepairs)
    }

}